package com.zhl.gradledemo.service.impl;

import com.zhl.gradledemo.mapper.ArticleMapper;
import com.zhl.gradledemo.model.Article;
import com.zhl.gradledemo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by Administrator on 2019/11/2.
 */
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public int addArticle(Article article) {
        return articleMapper.addArticle(article);
    }

    @Override
    public List<Article> articleList(int pageNumber, int pageSize, String articleType, String title) {


        /*Example example = new Example(Article.class);
        example.createCriteria().andEqualTo("articleType");
        example.orderBy("createTime").orderBy("id").desc();
        List<Article> articleList = articleMapper.selectByExampleAndRowBounds(example, new RowBounds(pageNumber, pageSize));*/

        int start = pageNumber * pageSize;
        List<Article> articleList = articleMapper.articleList(start, pageSize, articleType, title);
        return articleList;
    }

    @Override
    public Article queryArticleById(int id) {
        Example example = new Example(Article.class);
        example.createCriteria().andEqualTo("id", id);
        Article article = articleMapper.selectOneByExample(example);
//        Article article = articleMapper.queryArticleById(id);
        return article;
    }

    @Override
    public int updateArticle(Article article) {
        return articleMapper.updateArticle(article);
    }

    @Override
    public int getCount() {
        return articleMapper.getCount();
    }

    @Override
    public int deleteArticle(int id) {
        return 0;
    }
}
